﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

using XRpgLibrary;
using XRpgLibrary.Controls;
using XRpgLibrary.SpriteClasses;
using XRpgLibrary.TileEngine;
using XRpgLibrary.WorldClasses;
using XRpgLibrary.ItemClasses;
using XRpgLibrary.CharacterClasses;

using RpgLibrary.ItemClasses;
using RpgLibrary.CharacterClasses;
using RpgLibrary.SkillClasses;

using SourceOfMagic.Components;

namespace SourceOfMagic.GameScreens
{
    public class CharacterGeneratorScreen : BaseGameState
    {

        #region Field Region

        RightLeftSelector genderSelector;
        RightLeftSelector classSelector;
        PictureBox backgroundImage;
        PictureBox characterImage;
        Texture2D[,] characterImages;
        Texture2D containers;
        string[] genderItems = { "Male", "Female" };
        string[] classItems;
        
        #endregion
        
        #region Property Region

        public string SelectedGender
        {
            get { return genderSelector.SelectedItem; }
        }
        public string SelectedClass
        {
            get { return classSelector.SelectedItem; }
        }

        #endregion
        
        #region Constructor Region

        public CharacterGeneratorScreen(RpgGame game, GameStateManager stateManager)
            : base(game, stateManager)
        {
        }
        
        #endregion
        
        #region XNA Method Region
        
        public override void Initialize()
        {
            base.Initialize();
        }
        
        protected override void LoadContent()
        {
            base.LoadContent();

            LoadClasses();

            LoadImages();
            CreateControls();

            containers = Game.Content.Load<Texture2D>(@"ObjectSprites\containers");
        }

        private void LoadClasses()
        {
            classItems = new string[DataManager.CharacterData.Count];

            int counter = 0;
            foreach (string className in DataManager.CharacterData.Keys)
            {
                classItems[counter] = className;
                counter++;
            }
        }
        
        public override void Update(GameTime gameTime)
        {
            ControlManager.Update(gameTime, PlayerIndex.One);
            base.Update(gameTime);
        }
        
        public override void Draw(GameTime gameTime)
        {
            GameRef.spriteBatch.Begin();
            base.Draw(gameTime);
            ControlManager.Draw(GameRef.spriteBatch);
            GameRef.spriteBatch.End();
        }

        #endregion
        
        #region Method Region
        
        private void CreateControls()
        {
            Texture2D leftTexture = Game.Content.Load<Texture2D>(@"GUI\leftarrowUp");
            Texture2D rightTexture = Game.Content.Load<Texture2D>(@"GUI\rightarrowUp");
            Texture2D stopTexture = Game.Content.Load<Texture2D>(@"GUI\StopBar");
            
            backgroundImage = new PictureBox(
                Game.Content.Load<Texture2D>(@"Backgrounds\title-screen-temp"),
                GameRef.ScreenRectangle);
            ControlManager.Add(backgroundImage);
            
            Label label1 = new Label();
            label1.Text = "Choose Your Character";
            label1.Size = label1.SpriteFont.MeasureString(label1.Text);
            label1.Position = new Vector2((GameRef.Window.ClientBounds.Width - label1.Size.X) /
            2, 150);
            ControlManager.Add(label1);
            
            genderSelector = new RightLeftSelector(leftTexture, rightTexture, stopTexture);
            genderSelector.SetItems(genderItems, 125);
            genderSelector.Position = new Vector2(label1.Position.X, 200);
            genderSelector.SelectionChanged += new EventHandler(selectionChanged);
            ControlManager.Add(genderSelector);
            
            classSelector = new RightLeftSelector(leftTexture, rightTexture, stopTexture);
            classSelector.SetItems(classItems, 125);
            classSelector.Position = new Vector2(label1.Position.X, 250);
            classSelector.SelectionChanged += selectionChanged;
            ControlManager.Add(classSelector);
            
            LinkLabel linkLabel1 = new LinkLabel();
            linkLabel1.Text = "Accept this character.";
            linkLabel1.Position = new Vector2(label1.Position.X, 300);
            linkLabel1.Selected += new EventHandler(linkLabel1_Selected);
            ControlManager.Add(linkLabel1);
            
            characterImage = new PictureBox(
                characterImages[0, 0],
                new Rectangle(500, 200, 96, 96),
                new Rectangle(0, 0, 32, 32));
            ControlManager.Add(characterImage);

            ControlManager.NextControl();
        }

        private void LoadImages()
        {
            characterImages = new Texture2D[genderItems.Length, classItems.Length];
            for (int i = 0, len = genderItems.Length; i < len; i++)
            {
                for (int j = 0, clen = classItems.Length; j < clen; j++)
                {
                    characterImages[i, j] = Game.Content.Load<Texture2D>(@"PlayerSprites\" +
                    genderItems[i] + classItems[j]);
                }
            }
        }

        void linkLabel1_Selected(object sender, EventArgs e)
        {
            InputHandler.Flush();

            //CreatePlayer();
            //CreateWorld();

            GameRef.SkillScreen.SkillPoints = 10;
            TransitionChange(GameRef.SkillScreen);
            GameRef.SkillScreen.SetTarget(GamePlayScreen.Player);
        }

        void selectionChanged(object sender, EventArgs e)
        {
            characterImage.Image = characterImages[genderSelector.SelectedIndex, classSelector.SelectedIndex];
        }
        #endregion

        //private void CreatePlayer()
        //{
        //    Dictionary<AnimationKey, Animation> animations = new Dictionary<AnimationKey, Animation>();
            
        //    Animation animation = new Animation(3, 32, 32, 0, 0);
        //    animations.Add(AnimationKey.Down, animation);
            
        //    animation = new Animation(3, 32, 32, 0, 32);
        //    animations.Add(AnimationKey.Left, animation);
            
        //    animation = new Animation(3, 32, 32, 0, 64);
        //    animations.Add(AnimationKey.Right, animation);
            
        //    animation = new Animation(3, 32, 32, 0, 96);
        //    animations.Add(AnimationKey.Up, animation);
            
        //    AnimatedSprite sprite = new AnimatedSprite(
        //        characterImages[genderSelector.SelectedIndex, classSelector.SelectedIndex],
        //        animations, 
        //        GameRef.spriteBatch);

        //    Character character = new Character(DataManager.CharacterData[classSelector.SelectedItem]);
        //    character.Sprite = sprite;

        //    GamePlayScreen.Player = character; 
        //}

        
    }
}
